//=================================================
// M2M ARH
// 2014-02-19
//=================================================
// a = 97 read analog pin
// d = 100 read digital pin
// s = 115 set digital pin
// p = 112 set pinMode
//=================================================
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 12

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress device[10];
//==================================================
int PIN_MSG = 3;

int MAX_DIG_PIN = 13;
int MAX_ANA_PIN =  5;

int DIR   = 10;
int STEP  = 11;
int SLEEP = 12;

int a0,a1,a2,a3,a4,a5; 
int inByte1 = 0,inByte2 = 0,inByte3 = 0; // incoming serial byte
//=================================================
void blinkLed(int nn)
{
  digitalWrite(PIN_MSG,LOW);
  for(int i=0;i<nn;i++)
  { 
     digitalWrite(PIN_MSG,HIGH);
     delay(100);
     digitalWrite(PIN_MSG,LOW);
     delay(100);
  }  
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print("A.");   // send a capital A
    delay(300);
    blinkLed(1);
  }
}
//================================================
void stepCW(int steps)
//================================================
{
  int i;
  digitalWrite(DIR, LOW);                 
  digitalWrite(SLEEP, HIGH); // Set the Sleep mode to AWAKE.
  for(i=0;i<=steps;i++)
    {
      digitalWrite(STEP, HIGH); 
      delay(10);      
      digitalWrite(STEP, LOW);
      delay(10);  
    }  
  digitalWrite(DIR, LOW);   
  digitalWrite(SLEEP, LOW); // Set the Sleep mode to SLEEP.   
}

//================================================
void stepCCW(int steps)
//================================================
{
  int i;
  digitalWrite(DIR, HIGH);                 
  digitalWrite(SLEEP, HIGH); // Set the Sleep mode to AWAKE.
  for(i=0;i<=steps;i++)
    {
      digitalWrite(STEP, HIGH);   
      delay(10);       
      digitalWrite(STEP, LOW); 
      delay(10);     
    }  
  digitalWrite(DIR, LOW);   
  digitalWrite(SLEEP, LOW); // Set the Sleep mode to SLEEP.    
}
//=================================================
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
//=================================================
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}
//=================================================
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
//=================================================
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
}
//=================================================
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
//=================================================
{
  Serial.print("Resolution: ");
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();    
}
//=================================================
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
//=================================================
{
  //Serial.print("Device Address: ");
  //printAddress(deviceAddress);
  //Serial.print(" ");
  printTemperature(deviceAddress);
  //Serial.println();
}
int nsensors = 0;
//=================================================
void setup()
//=================================================
{
  int i;
  // start serial port at 9600 bps and wait for port to open:
  Serial.begin(9600);
  
  sensors.begin();
  nsensors = sensors.getDeviceCount();
  // locate devices on the bus
  //Serial.print("Locating devices...");
  //Serial.print("Found ");
  //Serial.print(nsensors, DEC);
  //Serial.println(" devices.");

  // report parasite power requirements
  if(nsensors > 0)
  {
    //Serial.print("Parasite power is: "); 
    //if (sensors.isParasitePowerMode()) Serial.println("ON");
    //else Serial.println("OFF");
    for(i=0;i<nsensors;i++)
    {
      sensors.getAddress(device[i], i);
      sensors.setResolution(device[i], TEMPERATURE_PRECISION);
      //if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
      //if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");  
    }
    //Serial.print("Device 0 Address: ");
    //printAddress(insideThermometer);
    //Serial.println();

    //Serial.print("Device 1 Address: ");
    //printAddress(outsideThermometer);
    //Serial.println();

    // set the resolution to 9 bit
    //sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
    //sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);

    //Serial.print("Device 0 Resolution: ");
    //Serial.print(sensors.getResolution(insideThermometer), DEC); 
    //Serial.println();

    //Serial.print("Device 1 Resolution: ");
    //Serial.print(sensors.getResolution(outsideThermometer), DEC); 
    //Serial.println();
  }
  
  pinMode(PIN_MSG, OUTPUT);
  for(i=4;i<=13;i++)pinMode(i, OUTPUT);
  digitalWrite(PIN_MSG,LOW);
  
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  //Serial.flush();
  establishContact();  // send a byte to establish contact until receiver responds 
}
//=================================================
void loop()
//=================================================
{
  int nx = Serial.available();
  int value = 777,i,temp;

  if (nx > 0) 
  {
     blinkLed(nx);
     inByte1 = Serial.read();
     inByte2 = Serial.read();  
     inByte3 = Serial.read();  
     inByte2 = inByte2 - 48;   
     inByte3 = inByte3 - 48;     
     
     //Serial.println(inByte1,DEC);
     //Serial.println(inByte2,DEC);


    //-------------------------
     if(inByte1 == 122) // stepper
     { 
       int direction = inByte2;
       int steps     = inByte3;
       if(steps > 0 || steps < 20)
       {
         if(direction == 1) stepCW(steps);
         if(direction == 2) stepCCW(steps);
       }
     }

     //-------------------------
     if(inByte1 == 100) // a read digital
     { 
       for(i=0;i<=MAX_DIG_PIN;i++)
       {
         if(inByte2 == i)
         {
            value = digitalRead(i);
            Serial.print(value,DEC);
            //Serial.print(" ");
            //Serial.println(inByte2,DEC);
         }
       }
     }
     
     //-------------------------
     if(inByte1 == 115) // s write digital
     { 
       for(i=2;i<=MAX_DIG_PIN;i++)
       {
         if(inByte2 == i)
         {
            digitalWrite(i,inByte3);
            //Serial.print(i,DEC);
            //Serial.print(" ");
            //Serial.println(inByte3,DEC);
         }
       }
     }
     //-------------------------
     if(inByte1 == 119) // s write analog pwm
     { 
       for(i=0;i<=MAX_DIG_PIN;i++)
       {
         if(inByte2 == 3 || inByte2 == 5 ||inByte2 == 6 ||inByte2 == 9 ||inByte2 == 10 || inByte2 == 11)
         {
            if(inByte3 >= 0 && inByte3 < 256)analogWrite(i,inByte3);
         }
       }
     }

     //-------------------------
     if(inByte1 == 97) // a read analog
     { 
       if(inByte2 == 0)
       {
          value = analogRead(A0);
       }
       if(inByte2 == 1)
       {
          value = analogRead(A1);
       }
       if(inByte2 == 2)
       {
          value = analogRead(A2);
       }
       if(inByte2 == 3)
       {
          value = analogRead(A3);
       }
       if(inByte2 == 4)
       {
          value = analogRead(A4);
       }
       if(inByte2 == 5)
       {
          value = analogRead(A5);
       }
       Serial.print(value,DEC);
     }
     //-------------------------
     if(inByte1 == 65) // DDRD digital pin 0-7
     { 
          Serial.print(DDRD,DEC);
     }
     if(inByte1 == 66) // DDRB digital pin 8-13
     { 
          Serial.print(DDRB,DEC);
     }
     if(inByte1 == 67) // DDRC analog pin 0 - 5
     { 
          Serial.print(DDRC,DEC);
     }
     //-------------------------
     if(inByte1 == 68) // PORTD digital pin 0-7
     { 
          Serial.print(PORTD,DEC);
     }
     if(inByte1 == 69) // PORTB digital pin 8-13
     { 
          Serial.print(PORTB,DEC);
     }
     if(inByte1 == 70) // PORTC analog pin 0 - 5
     { 
          Serial.print(PORTC,DEC);
     }
     //-------------------------
     if(inByte1 == 71) // PIND digital pin 0-7
     { 
          Serial.print(PIND,DEC);
     }
     if(inByte1 == 72) // PINB digital pin 8-13
     { 
          Serial.print(PINB,DEC);
     }
     if(inByte1 == 73) // PINC analog pin 0 - 5
     { 
          Serial.print(PINC,DEC);
     }
     //-------------------------
     if(inByte1 == 74) // ALL_PIN_STATUS
     { 
          value = analogRead(A0);Serial.print(value,DEC);Serial.print("+");
          value = analogRead(A1);Serial.print(value,DEC);Serial.print("+");
          value = analogRead(A2);Serial.print(value,DEC);Serial.print("+");
          value = analogRead(A3);Serial.print(value,DEC);Serial.print("+");
          value = analogRead(A4);Serial.print(value,DEC);Serial.print("+");
          value = analogRead(A5);Serial.print(value,DEC);Serial.print("+");
          Serial.print(DDRD,DEC);Serial.print("+");
          Serial.print(DDRB,DEC);Serial.print("+");
          Serial.print(DDRC,DEC);Serial.print("+");
          
          Serial.print(PORTD,DEC);Serial.print("+");
          Serial.print(PORTB,DEC);Serial.print("+");
          Serial.print(PORTC,DEC);Serial.print("+");
          
          Serial.print(PIND,DEC);Serial.print("+");
          Serial.print(PINB,DEC);Serial.print("+");
          Serial.print(PINC,DEC);Serial.print("+");
          Serial.print(nsensors,DEC);
          sensors.requestTemperatures();
          for(i=0;i<5;i++)
          {
           if(i<nsensors)
           {
             float tempC = sensors.getTempC(device[i]);
             temp = tempC*100;
           }
           else
             temp = 0;
           Serial.print("+");
           Serial.print(temp,DEC);
          }          
     }
     
     Serial.print('.');
     
  }

}
